﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DentScan2.DB;

namespace DentScan2.WebApp.Classes
{
    [Serializable]
    public class Owner
    {
        public Guid? OwnerId
        {
            get;
            set;
        }


        public string Name
        {
            get;
            set;
        }

        public string Address
        {
            get;
            set;
        }
        public string Identifier
        {
            get;
            set;
        }
        public Guid? CityId
        {
            get;
            set;
        }
        public string CityName
        {
            get;
            set;
        }

        public Guid? DistrictId
        {
            get;
            set;
        }
        public string DistrictName
        {
            get;
            set;
        }

        public Guid? CountryId
        {
            get;
            set;
        }
        public string CountryName
        {
            get;
            set;
        }

        public string ContactPerson
        {
            get;
            set;
        }
        public string ContactPhone
        {
            get;
            set;
        }
        public string MOL
        {
            get;
            set;
        }
        public string Activity
        {
            get;
            set;
        }
        public string PK
        {
            get;
            set;
        }
        public string Phones
        {
            get;
            set;
        }
        public string Faxes
        {
            get;
            set;
        }
        public string Emails
        {
            get;
            set;
        }

        public Owner()
        {
        }

        public Owner(DB.Owner owner)
        {
            this.Activity = owner.Activity;
            this.Address = owner.Address;
            this.CityId = owner.City.CityId;
            this.CityName = owner.City.CityName;
            this.ContactPerson = owner.ContactPerson;
            this.ContactPhone = owner.ContactPhone;
            this.CountryId = owner.City.District.Country.CountryId;
            this.CountryName = owner.City.District.Country.CountryName;
            this.DistrictId = owner.City.District.DistrictId;
            this.DistrictName = owner.City.District.DistrictName;
            this.Emails = owner.Emails;
            this.Faxes = owner.Faxes;
            this.Identifier = owner.Identifier;
            this.MOL = owner.MOL;
            this.Name = owner.Name;
            this.OwnerId = owner.OwnerId;
            this.Phones = owner.Phones;
            this.PK = owner.PK;
        }


        public void SaveToDB(DLDataContext dl)
        {
            if (this.OwnerId == null)
                Insert(dl);
            else
                Update(dl);
        }

        private void Update(DLDataContext dl)
        {
            var item = dl.Owners.Where(o => o.OwnerId == this.OwnerId).First();
            item.Activity = this.Activity;
            item.Address = this.Address;
            item.CityId = this.CityId;
            item.ContactPerson = this.ContactPerson;
            item.ContactPhone = this.ContactPhone;
            item.Emails = this.Emails;
            item.Faxes = this.Faxes;
            item.Identifier = this.Identifier;
            item.MOL = this.MOL;
            item.Name = this.Name;
            item.Phones = this.Phones;
            item.PK = this.PK;

            item.IsDel = false;
            item.LastEditDate = DateTime.UtcNow;
            dl.SubmitChanges();


        }

        private void Insert(DLDataContext dl)
        {
            var item = new DB.Owner();
            item.OwnerId = Guid.NewGuid();

            item.Activity = this.Activity;
            item.Address = this.Address;
            item.CityId = this.CityId;
            item.ContactPerson = this.ContactPerson;
            item.ContactPhone = this.ContactPhone;
            item.Emails = this.Emails;
            item.Faxes = this.Faxes;
            item.Identifier = this.Identifier;
            item.MOL = this.MOL;
            item.Name = this.Name;
            item.Phones = this.Phones;
            item.PK = this.PK;

            item.IsDel = false;
            item.LastEditDate = DateTime.UtcNow;
            dl.Owners.InsertOnSubmit(item);
            dl.SubmitChanges();
            this.OwnerId = item.OwnerId;
        }
    }
}